package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;

@Component
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        System.out.println("定时任务执行了============================================");
        //获取redis集合中保存的所有图片数据
        Set<String> smembers = jedisPool.getResource().smembers(RedisConstant.SETMEAL_PIC_RESOURCES);
        if(smembers==null || smembers.size()==0){
            return;
        }
        //从七牛云删除图片数据
        smembers.stream().forEach(img -> {
            //从七牛云删除图片
            QiniuUtils.deleteFileFromQiniu(img);
            // 从redis中删除图片
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, img);
        });
    }
}
